# -*- coding: utf-8 -*-

import os
import sys
import time
import subprocess
import syslog
import ConfigParser

syslog.openlog('NDP')

def log_msg(msg):
	sys.stdout.write(msg + '\n')
	syslog.syslog(msg)

	return msg

def req_option(option):
	read_ok = 0

	while not read_ok:
		config = ConfigParser.ConfigParser()
		config.read('/etc/ndpd.cfg')

		if not config.has_section('main'):
			log_msg('Unable to read "main" section from configuration file, create it, please!')

			time.sleep(5)
			continue

		if not config.has_option('main', option):
			log_msg('Unable to read "main:%s" from configuration file!' % option)

			time.sleep(5)
			continue

		value = config.get('main', option)
		read_ok = 1

	return value

def call_script(ip_addr, ok):
	return subprocess.call([req_option('script_path'), ip_addr, str(ok)])

